﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using WeManageService;
using System.Drawing.Drawing2D;

namespace WeManageForm
{
    public partial class LoginForm : Form
    {
        public static List<User> users;
        UserManager userManager = new UserManager();
        public Point mouseDownPoint;
        public bool CBUserClick = false;

        public LoginForm()
        {
            users = userManager.Users;
            this.StartPosition = FormStartPosition.CenterParent;
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            initLoginInfo();
            CMS.Opacity = 0.65;
            Image image = PicUser.BackgroundImage;
            image = CutEllipse(image, new Rectangle(0, 0, 186, 186), new Size(186, 186));
            PicUser.BackgroundImage = image;
            this.BringToFront();
        }

        private void initLoginInfo()
        {
            bool done = false;
            foreach (User user in users)
            {
                CBUserName.Items.Add(user.UserName);
                //string PassWord = "" + (user.AutoLogin == true ? 1 : 0) + ((user.Remember == true ? 1 : 0)) + user.PassWord.ToString();
                //this.dt.Rows.Add(new string[] { user.UserName, PassWord });
                if (!done && (user.AutoLogin || user.Remember))
                {                    
                    CBUserName.Text = user.UserName;
                    TxtPassword.Text = user.PassWord;
                    CBRemember.Checked = user.Remember;
                    CBAutoLogin.Checked = user.AutoLogin;
                    done = true;
                }
            }
        }

        private void CBUserName_SelectedIndexChanged(object sender, EventArgs e)
        {
            User user = users[CBUserName.SelectedIndex];
            if (user.AutoLogin || user.Remember)
                TxtPassword.Text = user.PassWord;
            else
                TxtPassword.Text = "";
            CBRemember.Checked = user.Remember;
            CBAutoLogin.Checked = user.AutoLogin;
        }

        private void CBUserName_MouseClick(object sender, MouseEventArgs e)
        {
            if (!CBUserClick)
            {
                CBUserClick = true;
                CBUserName.SelectAll();
            }
            else
            {
                CBUserName.Select();
            }
        }

        private void CBUserName_TextChanged(object sender, EventArgs e)
        {
            int index = CBUserName.Items.IndexOf(CBUserName.Text);
            if (index != -1)
            {
                User user = users[index];
                if (user.AutoLogin || user.Remember)
                    TxtPassword.Text = user.PassWord;
                CBRemember.Checked = user.Remember;
                CBAutoLogin.Checked = user.AutoLogin;
            }
            else
            {
                TxtPassword.Text = "";
                CBRemember.Checked = false;
                CBAutoLogin.Checked = false;
            }
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            User user = new User();
            user.UserName = CBUserName.Text;
            user.PassWord = TxtPassword.Text;
            user.Remember = CBRemember.Checked;
            user.AutoLogin = CBAutoLogin.Checked;
            if (CBUserName.Text == "" || TxtPassword.Text == "") { }
            else if (userManager.Login(user))
            {
                userManager.SetLoginWay(user);
                Global.UserName = CBUserName.Text;
                PlanManager.Initialrize(Global.UserName);
                WishManager.Initialrize(Global.UserName);
                TurnoverManager.Initialrize(Global.UserName);
                this.Close();
                this.NotifyIcon.Dispose();
                MainForm mainForm = new MainForm();
                mainForm.Show();
            }
            else
            {
                if (userManager.GetUser(user.UserName) == null)
                {
                    FailForm failForm = new FailForm(false);
                    failForm.ShowDialog();
                }
                else
                {
                    FailForm failForm = new FailForm(true);
                    failForm.ShowDialog();
                }
            }

        }

        private void LoginForm_FormClosed(object sender, EventArgs e)
        {
            this.NotifyIcon.Dispose();
        }

        private void CBAutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            if (CBAutoLogin.Checked)
            {
                CBRemember.Checked = true;
            }
            else
            {
                CBRemember.Checked = false;
            }
        }

        private void LblRegister_Click(object sender, EventArgs e)
        {
            this.Hide();
            RegisterForm registerForm = new RegisterForm();
            registerForm.ShowDialog();
            this.Show();
        }

        private void LblRegister_MouseHover(object sender, EventArgs e)
        {
            LblRegister.ForeColor = Color.Red;
        }

        private void LblRegister_MouseLeave(object sender, EventArgs e)
        {
            LblRegister.ForeColor = Color.SteelBlue;
        }

        private void LoginForm_FormLoad(object sender, EventArgs e)
        {

        }

        private void LblFindP_MouseHover(object sender, EventArgs e)
        {
            LblFindPassword.ForeColor = Color.Red;
        }

        private void LblFindP_MouseLeave(object sender, EventArgs e)
        {
            LblFindPassword.ForeColor = Color.SteelBlue;
        }

        private void CBRemember_CheckedChanged(object sender, EventArgs e)
        {
            if (!CBRemember.Checked)
            {
                CBAutoLogin.Checked = false;
            }
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            System.Environment.Exit(0);
            NotifyIcon.Dispose();
        }

        private void LoginForm_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDownPoint = new Point(e.X, e.Y);
        }

        private void LoginForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mouseDownPoint.X,
                    this.Location.Y + e.Y - mouseDownPoint.Y);
            }
        }

        private void BtnMinSize_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Minimized;
        }

        private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = true; //显示在系统任务栏 
                this.WindowState = FormWindowState.Normal; //还原窗体
            }
        }

        private void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            this.Show();
            if (e.Button == MouseButtons.Left)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    this.ShowInTaskbar = true; //显示在系统任务栏 
                    this.WindowState = FormWindowState.Normal; //还原窗体
                }
            }
        }

        private void 打开主页版ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            System.Environment.Exit(0);
        }

        private void LblFindP_Click(object sender, EventArgs e)
        {
            this.Hide();
            FindPasswordForm findPasswordForm = new FindPasswordForm(CBUserName.Text);
            findPasswordForm.ShowDialog();
            this.Show();
        }

        private Image CutEllipse(Image img, Rectangle rec, Size size)
        {
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
                {
                    br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.FillEllipse(br, new Rectangle(Point.Empty, size));
                }
            }
            bitmap = ZoomImage(bitmap, 80, 80);
            return bitmap;
        }

        private Bitmap ZoomImage(Bitmap bitmap, int destHeight, int destWidth)
        {
            try
            {
                System.Drawing.Image sourImage = bitmap;
                int width = 0, height = 0;
                //按比例缩放             
                int sourWidth = sourImage.Width;
                int sourHeight = sourImage.Height;
                if (sourHeight > destHeight || sourWidth > destWidth)
                {
                    if ((sourWidth * destHeight) > (sourHeight * destWidth))
                    {
                        width = destWidth;
                        height = (destWidth * sourHeight) / sourWidth;
                    }
                    else
                    {
                        height = destHeight;
                        width = (sourWidth * destHeight) / sourHeight;
                    }
                }
                else
                {
                    width = sourWidth;
                    height = sourHeight;
                }
                Bitmap destBitmap = new Bitmap(destWidth, destHeight);
                Graphics g = Graphics.FromImage(destBitmap);
                g.Clear(Color.Transparent);
                //设置画布的描绘质量           
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
                g.Dispose();
                //设置压缩质量       
                System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
                long[] quality = new long[1];
                quality[0] = 100;
                System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                encoderParams.Param[0] = encoderParam;
                sourImage.Dispose();
                return destBitmap;
            }
            catch
            {
                return bitmap;
            }
        }


        private void LblLine1_MouseHover(object sender,EventArgs e)
        {
            LblLine1.ForeColor = Color.SteelBlue;
        }
        private void LblLine1_MouseLeave(object sender, EventArgs e)
        {
            LblLine1.ForeColor = Color.Aqua;
        }

        private void LblLine2_MouseHover(object sender, EventArgs e)
        {
            LblLine2.ForeColor = Color.SteelBlue;
        }
        private void LblLine2_MouseLeave(object sender, EventArgs e)
        {
            LblLine2.ForeColor = Color.Aqua;
        }
        
        //作用 加快界面加载 解决闪屏
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
    }
}

